var cheerio = require('cheerio');
var request = require('request');
var async = require('async');
var request = request.defaults({
  jar: true
})
module.exports = {
  getWX: function (wx, callback) {
    // 'http://www.gsdata.cn/query/wx?q='+wx
    // request({
    //   method: 'GET',
    //   uri: 'http://www.gsdata.cn/query/wx?q=' + encodeURIComponent(wx),
    //   headers: {
    //     'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36'
    //   }
    // }, function (error, response, body) {
    // var $ = cheerio.load(body);
    // var url = $("#nickname").attr('href');
    // url = "bQGBVDuSZJ3qRivnbgw5O0O0O2O0O0O1"
    // console.log(response.headers)
    // if (!!url) {
      // var wxname = url.split('=')[1];
      var wxname = "bQGBVDuSZJ3qRivnbgw5O0O0O2O0O0O1"
      console.log('data:', wxname)
      request({
        method: 'POST',
        uri: 'http://www.gsdata.cn/rank/toparc?wxname=' + wxname + '&sort=-1&wx=lengtoo',
        headers: {
          // 'Host': 'www.gsdata.cn',
          // 'Origin': 'http://www.gsdata.cn',
          'X-Requested-With': 'XMLHttpRequest',
          // 'Referer': 'http://www.gsdata.cn/rank/wxdetail?wxname=bQGBVDuSZJ3qRivnbgw5O0O0O2O0O0O1',
          'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36'
        }
      }, function (error, response, data) {
        console.log('data:', data, response)
        var lists = JSON.parse(data)
        callback(lists)
      })
      // } else {
      //   lists = {
      //     error: 1,
      //     data: []
      //   }
      //   callback(lists)
    // }
    // })
  }
}
